﻿var xmldom
var PlayerColors=new Array()
PlayerColors[0]="#e69595";
PlayerColors[1]="#95aae6";
PlayerColors[2]="#95e6b3";
PlayerColors[3]="#5cb8ca";
PlayerColors[4]="#b395e6";
PlayerColors[5] = "#d88aec";

var CurrentSeat = "-1";

var RoomPlayers=new Array()
RoomPlayers[0]="";
RoomPlayers[1]="";
RoomPlayers[2]="";
RoomPlayers[3]="";
RoomPlayers[4]="";
RoomPlayers[5] = "";

var RoomSeats = new Array()
RoomSeats[0] = "";
RoomSeats[1] = "";
RoomSeats[2] = "";
RoomSeats[3] = "";
RoomSeats[4] = "";
RoomSeats[5] = "";

var Lastprid = "0";
var Lastchatid = "0";

var V_playrecord = "";
var V_room = "";
var V_round = "";
var V_ucoin = "";
var V_uroom = "";
var V_uinfo = "";
var V_chat = "";
var V_adminroom = "";
var V_roundresult = "";

var CurrentRoundGUID="";
var R_LastPlayerTime;
var R_LastArrageCardID="";
var R_CardCount=0;
var R_CurrentPlayer="";
var R_LastCardPlayer="";
var R_EndRecordID=0;
var R_UpdateTime;
var R_LastType=""
var R_Status=""
var R_EndTime;
var R_CurrentNickName="";
var R_LastTime;

var ServerTime;

var getRoomOk=true;


$(document).ready(function(){
    getRoom();
    timer = setInterval(getRoom,1000);
});

function getRoom() {
    if(!getRoomOk)
        return;
    getRoomOk=false;
    var datastr=""
    if ($(xmldom).find("Room").length != 1) {
        datastr = "opName=GetRoom&roomguid=" + CurrentRoomGUID + "&prid=0&chatid=0&ran="+Math.random();
    }
    else {
        var roomversion = $($(xmldom).find("RoomVersion")[0]);
        datastr = "opName=GetRoom&roomguid=" + CurrentRoomGUID + "&prid=" + Lastprid + "&chatid=" + Lastchatid + "&vplayrecord=" + V_playrecord + "&vroom=" + V_room + "&vround=" + V_round + "&vucoin=" + V_ucoin + "&vuroom=" + V_uroom + "&vuinfo=" + V_uinfo + "&vchat=" + V_chat + "&vadminroom=" + V_adminroom + "&vroundresult=" + V_roundresult+"&ran="+Math.random();
    }
    try
    {
    $.ajax({
        url: 'ajaxroom.aspx',
        type: 'GET',
        dataType: 'xml',
        contentType: "application/x-www-form-urlencoded; charset=utf-8", 
        timeout: 5000,
        data: datastr,
        error: function() {
            //alert('网络异常');
            getRoomOk=true;
        },
        success: function(xml) {
            getRoomOk=true;
            xmldom = xml;
            if($(xmldom).find("Result").text()=="-90015")
            {
                //不在该房间
                window.location.href="main.aspx";
            }
            ServerTime=ConvertDate($(xmldom).find("ServerTime").text());
            ShowUI();
            //最后updateversion
            UpdateVersion($($(xmldom).find("RoomVersion")[0]));
        }
    });
    }
    catch(e)
    {
        getRoomOk=true;
    }
}

function UpdateVersion(roomversion) {
    V_playrecord = roomversion.children("vplayrecord").text();
    V_room = roomversion.children("vroom").text();
    V_round = roomversion.children("vround").text();
    V_ucoin = roomversion.children("vucoin").text();
    V_uroom = roomversion.children("vuroom").text();
    V_uinfo = roomversion.children("vuinfo").text();
    V_chat = roomversion.children("vchat").text();
    V_adminroom = roomversion.children("vadminroom").text();
    V_roundresult = roomversion.children("vroundresult").text();
}

function ClearVersion() {
    V_playrecord = "";
    V_room = "";
    V_round = "";
    V_ucoin = "";
    V_uroom = "";
    V_uinfo = "";
    V_chat = "";
    V_adminroom = "";
    V_roundresult = "";
}

function ShowUI() { 
    //需在pr,PlayerList前
    ShowRoundInfo();
    ShowPRecord();
    //需在empty前
    ShowPlayerList();
    ShowEmptySeatInfo();
    ShowRoundResultInfo();
    ShowChatRecord();
}

function ShowRoundResultInfo()
{
    if($(xmldom).find("RoundResult").length>0&&R_Status=="1")
    {
        try{
            //alert(ServerTime);
            //alert(R_EndTime);
            if(ServerTime-R_EndTime<5000&&ServerTime-R_EndTime>-1)
            {
                $("#Result_ContentTable").empty();
                if(CurrentCoinID>0)
                {
                    $("#Result_ContentTable").append("<tr><td>昵称</td><td>积分</td><td>游戏币</td></tr>")
                    $(xmldom).find("RoundResult").each(function(){
                        $("#Result_ContentTable").append("<tr><td>"+$(this).children("NickName").text()+"</td><td>"+$(this).children("Score").text()+"</td><td>"+$(this).children("Coin").text()+"</td></tr>")
                    })
                }
                else
                {
                    $("#Result_ContentTable").append("<tr><td>昵称</td><td>积分</td></tr>")
                    $(xmldom).find("RoundResult").each(function(){
                        $("#Result_ContentTable").append("<tr><td>"+$(this).children("NickName").text()+"</td><td>"+$(this).children("Score").text()+"</td></tr>")
                    })
                }
                $("#CenterResultDiv").show();
                setTimeout(hideResult,5000);
            }
            else
            {
            }
        }
        catch(e)
        {}
    }
}

function hideResult()
{
    $("#CenterResultDiv").fadeOut(2000);
}

function ShowPRecord()
{
    //lastseat="";
    $(xmldom).find("PlayRecord").each(function(){
        if($(this).children("OperateType").text()=="2")
        {
            $('#CardList').append("您起牌："+$(this).children("CardDesc").text()+"</br>")
        }
        else if($(this).children("OperateType").text()=="1")
        {
            if($(this).children("NickName").text()==CurrentUser)
                $('#CardList').append("您：不要</br>")
            else
                $('#CardList').append($(this).children("NickName").text()+"：不要</br>")
        }
        else if($(this).children("OperateType").text()=="0")
        {
            if($(this).children("NickName").text()==CurrentUser)
                $('#CardList').append("您出牌："+$(this).children("CardDesc").text()+"</br>")
            else
                $('#CardList').append($(this).children("NickName").text()+"出牌："+$(this).children("CardDesc").text()+"</br>")
        }
//        if(R_EndRecordID==parseInt($(this).children("RecordID").text())&&lastseat!="")
//        {
//            //起牌，则上一位子的人ＰＡＳＳ
//            //显示pass
//            $("#Seat"+lastseat+"_ChatContentTD").html("PASS")
//            $("#Seat"+lastseat+"_ChatPop").show();
//            $("#Seat"+lastseat+"_ChatPop").fadeOut(3000);   
//        }
        if((R_EndRecordID<parseInt($(this).children("RecordID").text())||R_Status==1)&&$(this).children("OperateType").text()=="0"&&ServerTime-ConvertDate($(this).children("InsertTime").text())<5000)
        {
            $('#Seat'+$(this).children("SeatID").text()+'_OutCardsDiv').empty();
            returncardids=$(this).children("CardID").text();
            returncards=returncardids.split(",");
            for(j=0;j<returncards.length;j++)
            {
                tempoutcardsstr="<div class='CurrentUserOutCards' style='left:"+(j*14).toString()+"px;'><img src='/images/cards/id/"+returncards[j]+"_s.gif' alt='') /></div>"
                $('#Seat'+$(this).children("SeatID").text()+'_OutCardsDiv').append(tempoutcardsstr)
            }
        }
        //else if((R_EndRecordID<parseInt($(this).children("RecordID").text())||R_Status==1)&&$(this).children("OperateType").text()=="1")
        else if($(this).children("OperateType").text()=="1"&&ServerTime-ConvertDate($(this).children("InsertTime").text())<5000)
        {
            //显示pass
            $("#Seat"+$(this).children("SeatID").text()+"_ChatContentTD").html("PASS")
            $("#Seat"+$(this).children("SeatID").text()+"_ChatPop").show();
            $("#Seat"+$(this).children("SeatID").text()+"_ChatPop").fadeOut(3000);   
        }
        //lastseat=$(this).children("SeatID").text();
        Lastprid=$(this).children("RecordID").text()
    })
    if($(xmldom).find("PlayRecord").length>0)
    {
        //setTimeout(ClearPass,5000);
        if(R_EndRecordID>=$($(xmldom).find("PlayRecord")[$(xmldom).find("PlayRecord").length-1]).children("RecordID").text()&&R_Status!=1)
        {
            ClearOutCards()
        }
        if(R_Status==1)
        {
            setTimeout(ClearOutCards,5000)
            //显示结果
        }
    }
    if($(xmldom).find("PlayRecord").length>0)
        window.document.getElementById("CardList").scrollTop=9999;
}

function ClearOutCards()
{
    for(i=0;i<6;i++)
    {
        $('#Seat'+i+'_OutCardsDiv').empty();
    }
}

function ClearPass()
{
    for(i=0;i<6;i++)
    {
        if($("#Seat"+i+"_ChatContentTD").html()!="")
        {
            $("#Seat"+i+"_ChatContentTD").html("");
            $("#Seat"+i+"_ChatPop").hide(); 
        }
    }
}

function ShowChatRecord()
{
    $(xmldom).find("ChatRecord").each(function(){
        $('#ChatList').append($(this).children("NickName").text()+"说："+$(this).children("ChatContent").text()+"</br>")
        Lastchatid=$(this).children("ChatID").text()
        for(i=0;i<6;i++)
        {
            if(RoomSeats[i]==$(this).children("UserID").text())
            {
                //显示chat
                $("#Seat"+i.toString()+"_ChatContentTD").html($(this).children("ChatContent").text())
                $("#Seat"+i.toString()+"_ChatPop").show();
                $("#Seat"+i.toString()+"_ChatPop").fadeOut(3000); 
            }  
        }
    })
    if($(xmldom).find("ChatRecord").length>0)
        window.document.getElementById("ChatList").scrollTop=9999;
}

function initSeatArray() {
    RoomSeats[0] = "";
    RoomSeats[1] = "";
    RoomSeats[2] = "";
    RoomSeats[3] = "";
    RoomSeats[4] = "";
    RoomSeats[5] = "";
    RoomPlayers[0] = "";
    RoomPlayers[1] = "";
    RoomPlayers[2] = "";
    RoomPlayers[3] = "";
    RoomPlayers[4] = "";
    RoomPlayers[5] = "";
}

function strToDate(str)
{
    var val=Date.parse(str);
    var newDate=new Date(val);
    return newDate;
}

function ShowRoundInfo() {
    var theround = $($(xmldom).find("round")[0]);
    if (theround.children("RoundID").text() != ""&&theround.children("Refresh").text() == "1") {
        //如果是当前player则显示button
        if (theround.children("CurrentPlayer").text() == CurrentUser) {
//            if (CurrentSeat == "-1") {
//                raiseError(-99999);
//                return;
//            }
            $('#Seat' + CurrentSeat + '_CardButtonDiv').show();
            $('#Seat' + CurrentSeat + '_CardButtonDiv_start').show();
        }
        if(theround.children("Status").text()=="3")
        {
            tempcardcount=parseInt(theround.children("CardCount").text());
            if(tempcardcount==0)
            {
                temcardbackhtml="剩余"+theround.children("CardCount").text()+"张";
            }
            if(tempcardcount>0&&tempcardcount<5)
            {
                temcardbackhtml="<img src='/images/cards/back"+theround.children("CardCount").text()+".gif' alt='剩余"+theround.children("CardCount").text()+"张' /><br/>剩余"+theround.children("CardCount").text()+"张";
            }
            else if(tempcardcount>=5)
            {
                temcardbackhtml="<img src='/images/cards/back5.gif' alt='剩余"+theround.children("CardCount").text()+"张' /><br/>剩余"+theround.children("CardCount").text()+"张";
            }
            temcardbackhtml+="<br/>当前出牌人："+theround.children("CurrentNickName").text();
            $("#CenterCardBack").html(temcardbackhtml);
            $("#CurrentPlayerInfo").show()
        }
        else
        {
            $("#CenterCardBack").html("");
            $("#CurrentPlayerInfo").hide();
        }
        CurrentRoundGUID=theround.children("RoundID").text();
        R_LastPlayerTime=ConvertDate(theround.children("LastPlayerTime").text());
        R_LastArrageCardID=theround.children("LastArrageCardID").text();
        R_CardCount=parseInt(theround.children("CardCount").text());
        R_CurrentPlayer=theround.children("CurrentPlayer").text();
        R_LastCardPlayer=theround.children("LastCardPlayer").text();
        R_EndRecordID=parseInt(theround.children("EndRecordID").text());
        R_UpdateTime=ConvertDate(theround.children("UpdateTime").text());
        R_LastType=theround.children("LastType").text();
        R_Status=theround.children("Status").text();
        R_EndTime=ConvertDate(theround.children("EndTime").text());
        R_CurrentNickName=theround.children("CurrentNickName").text();
        R_LastTime=ConvertDate(theround.children("LastTime").text());
    }
    if(R_Status=="3")
    {
        tempLeftSecond=60-Math.round((ServerTime-R_LastTime)/1000);
        if(tempLeftSecond<0)
            tempLeftSecond=0;
        $("#LeftSecondSpan").html(tempLeftSecond)
    }
}

function ShowPlayerList() {
    if($(xmldom).find("Player").length >0) {
        initSeatArray();
        $('#PlayerList').empty();
        CurrentSeat = "-1";
        $(xmldom).find("Player").each(function(){
            $('#PlayerList').append("玩家"+$(this).children("NickName").text()+"</br>状态："+$(this).children("StatusDesc").text()+"</br></br>");
            if($(this).children("CoinName").text()!=""&&$(this).children("CoinCount").text()!=""&&$(this).children("UserID").text()==CurrentUser)
            {
                //游戏币
                $("#HeadCoinCountSpan").html("，数量："+$(this).children("CoinCount").text())
            }
            ShowPlayerSeatInfo($(this));
        })
    }
}

function ShowPlayerSeatInfo(player)
{
    if(player.children("SeatID").text()!="-1")
    {
        tempseatid=player.children("SeatID").text();
        tempvalue=player.children("UserID").text()+"|"+player.children("Status").text()+"|"+tempseatid+"|"+player.children("LeftCardID").text()+"|"+player.children("NickName").text()+"|"+player.children("HaveHeadImg").text()+"|"+player.children("UserScore").text()+"|"+player.children("NetStatus").text()+"|"+player.children("CoinCount").text()+"|"+player.children("CoinName").text()+"|"+player.children("CardNum").text();
        if(tempvalue!= RoomPlayers[parseInt(tempseatid)])
        {
            //有变化,重置该位置
            //用户信息
            tempCardUserDivHtml="昵称:"+player.children("NickName").text()+"<br />积分:"+player.children("UserScore").text();
            if(player.children("CoinName").text()!="")
            {
                if(tempseatid=="1"||tempseatid=="2")
                    tempCardUserDivHtml+="&nbsp;&nbsp;&nbsp;&nbsp;";
                else
                    tempCardUserDivHtml+="<br />";
                tempCardUserDivHtml+=player.children("CoinName").text()+":"+player.children("CoinCount").text()
            }
            if(player.children("UserID").text()==CurrentUser)
            {
                tempCardUserDivHtml="<span style='color:#95e6b3'>"+tempCardUserDivHtml+"<span>";
            }
            $('#Seat'+tempseatid+'_CardUserDiv').html(tempCardUserDivHtml);
            $("#Seat" + tempseatid + "_ArrowDiv").hide();
            //头像
            if(player.children("HaveHeadImg").text()=="true")
                $('#Seat'+tempseatid+'_CardHeadDiv').find("img").attr("src", "/images/head/"+player.children("UserID").text()+".gif");
            else
                $('#Seat'+tempseatid+'_CardHeadDiv').find("img").attr("src", "/images/head/default.gif");
            //牌
            $('#Seat'+tempseatid+'_CardCardsDiv').empty();
            if(player.children("CardNum").text()!="0")
            {
                if(player.children("UserID").text()==CurrentUser) {
                    CurrentSeat = tempseatid;
                    for(j=0;j<parseInt(player.children("CardNum").text());j++)
                    {
                        tempusercardsstr="<div class='CurrentUserCards' style='left:"+(j*14).toString()+"px;' cardid='"+player.children("LeftCardID").text().split(",")[j]+"'><img src='/images/cards/id/"+player.children("LeftCardID").text().split(",")[j]+"_s.gif' alt='' id='Seat"+tempseatid+"_Card_"+player.children("LeftCardID").text().split(",")[j]+"' onclick=CardMove('Seat"+tempseatid+"_Card_"+player.children("LeftCardID").text().split(",")[j]+"') /></div>"
                        $('#Seat'+tempseatid+'_CardCardsDiv').append(tempusercardsstr)
                    }
                }
                else
                { 
                    cardnum=player.children("CardNum").text();
                    $('#Seat'+tempseatid+'_CardCardsDiv').html("<img src='/images/cards/back"+cardnum+".gif' alt='剩余"+cardnum+"张' /><br/>&nbsp;&nbsp;剩余"+cardnum+"张&nbsp;&nbsp;&nbsp;");
                }
            }
            $("#Seat" + tempseatid + "_PlayerDiv").show();
            //按钮
            if(player.children("UserID").text()==CurrentUser)
            {
                if(player.children("Status").text()=="0")
                {
                    $("#Seat" + tempseatid + "_CardButtonDiv").hide();
                    $("#Seat" + tempseatid + "_CardButtonDiv_start").hide();
                    $("#Seat" + tempseatid + "_CardButtonDiv_stop").hide();
                }
                else if(player.children("Status").text()=="1")
                {
                    $("#Seat" + tempseatid + "_CardButtonDiv").show();
                    $("#Seat" + tempseatid + "_CardButtonDiv_start").hide();
                    $("#Seat" + tempseatid + "_CardButtonDiv_stop").show();
                    $("#Seat" + tempseatid + "_CardStart").show();
                    $("#Seat" + tempseatid + "_CardStand").show();
                }
                else if(player.children("Status").text()=="2")
                {
                    $("#Seat" + tempseatid + "_CardButtonDiv").show();
                    $("#Seat" + tempseatid + "_CardButtonDiv_start").hide();
                    $("#Seat" + tempseatid + "_CardButtonDiv_stop").show();
                    $("#Seat" + tempseatid + "_CardStart").hide();
                    $("#Seat" + tempseatid + "_CardStand").show();
                }
                else if(player.children("Status").text()=="3")
                {
                    $("#Seat" + tempseatid + "_CardButtonDiv").show();
                    if(R_CurrentPlayer==CurrentUser)
                        $("#Seat" + tempseatid + "_CardButtonDiv_start").show();
                    else
                        $("#Seat" + tempseatid + "_CardButtonDiv_start").hide();
                    $("#Seat" + tempseatid + "_CardButtonDiv_stop").hide();
                }
            }
            //准备
            if(player.children("Status").text()=="2"&&player.children("UserID").text()!=CurrentUser)
            {
                $("#Seat"+tempseatid+"_ReadyDiv").show();
            }
            else
            {
                $("#Seat"+tempseatid+"_ReadyDiv").hide();
            }
            RoomSeats[parseInt(tempseatid)] = player.children("UserID").text();
            RoomPlayers[parseInt(tempseatid)]=tempvalue;
        }
    }
}

function ShowEmptySeatInfo()
{
    for (i=0;i<6;i++)
    {
        if (RoomSeats[i] == "") {
            initUserSeat(i)
        }
    }
}

function initUserSeat(n) {
    $('#Seat' + n.toString() + '_CardHeadDiv').find("img").attr("src", "/images/head/loading37.gif");
    $("#Seat" + i.toString() + "_ArrowDiv").show();
    $("#Seat" + i.toString() + "_PlayerDiv").hide();
    $("#Seat" + i.toString() + "_CardButtonDiv_start").hide();
    $("#Seat" + i.toString() + "_CardButtonDiv_stop").hide();
    $("#Seat" + i.toString() + "_CardUserDiv").html("");
    $("#Seat" + i.toString() + "_CardCardsDiv").html("");
}
$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
          return false;
    });
});


$(function(){
$('body').mousedown(function(e){
if(e.which==3) // 1 = 鼠标左键 left; 2 = 鼠标中键; 3 = 鼠标右键
{
    for(i=0;i<6;i++)
    {
        if(CurrentUser!=""&&RoomSeats[i]==CurrentUser)
        {
            try{
//                cardids="";
//                $("#Seat" + i.toString() + "_CardCardsDiv").find("div").each(function(){
//                    if($(this).css("top")=="-10px")
//                        cardids=$(this).attr("cardid")+","+cardids;
//                });
//                if(cardids!="")
//                    Cardout(i);
//                else
//                    Cardpass(i);
                Cardout(i);
            }
            catch(e)
            {}
        }  
    }
}    
return false;//阻止链接跳转
})
})

function Cardout(n)
{
    cardids="";
    $("#Seat" + n.toString() + "_CardCardsDiv").find("div").each(function(){
        if($(this).css("top")=="-10px")
            cardids=$(this).attr("cardid")+","+cardids;
    });
    if(cardids!="")
        cardids=cardids.substring(0,cardids.length-1);
    else
        return;
    //$("#Seat" + n.toString() + "_CardButtonDiv").hide();
    $.ajax({
        url: 'ajaxroom.aspx',
        type: 'GET',
        dataType: 'json',
        contentType: "application/x-www-form-urlencoded; charset=utf-8", 
        timeout: 5000,
        data: "opName=Discard&roomguid=" + CurrentRoomGUID+"&cardids="+cardids+"&ArrageCardID=&RoundGUID="+CurrentRoundGUID+"&lasttype="+R_LastType+"&lastcardids="+R_LastArrageCardID+"&ran="+Math.random(),
        error: function() {
            $("#Seat" + n.toString() + "_CardButtonDiv").show();
        },
        success: function(result) {
            if (result.result.substring(0,1) == "0") {
                $('#Seat'+n.toString()+'_OutCardsDiv').empty();
                returncardids=result.result.substring(2);
                returncards=returncardids.split(",");
                for(j=0;j<returncards.length;j++)
                {
                    tempoutcardsstr="<div class='CurrentUserOutCards' style='left:"+(j*14).toString()+"px;'><img src='/images/cards/id/"+returncards[j]+"_s.gif' alt='') /></div>"
                    $('#Seat'+n.toString()+'_OutCardsDiv').append(tempoutcardsstr)
                }
                outcards=cardids.split(",");
                for(i=0;i<outcards.length;i++)
                {
                    $('#Seat'+n.toString()+'_CardCardsDiv').find("[cardid='"+outcards[i]+"']").remove();
                }
                $('#Seat' + n.toString() + '_CardButtonDiv').hide();
                $('#Seat' + n.toString() + '_CardButtonDiv_start').hide();
                $("#Seat" + n.toString() + "_CardButtonDiv").hide();
            }
            else if(result.result.substring(0,1) == "1")
            {
                $('#Seat'+n.toString()+'_OutCardsDiv').empty();
                tempoutcardsstr="";
                handcardsstr=result.result.substring(2);
                handcards=handcardsstr.split("|");
                for(i=0;i<handcards.length;i++)
                {
                    tempoutcardsstr+="<div class='CurrentUserOutCards' style='top:-20px;width:150px;'>请点击您要出的牌</div><div class='CurrentUserOutCards' style='top:"+(i*30).toString()+"px' onclick='chooseconfirmdiscard("+n.toString()+",\""+cardids+"\",\""+handcards[i]+"\")'>"
                    returncards=handcards[i].split(",");
                    for(j=0;j<returncards.length;j++)
                    {
                        tempoutcardsstr+="<div class='CurrentUserOutCards' style='left:"+(j*14).toString()+"px;'><img src='/images/cards/id/"+returncards[j]+"_s.gif' alt='') /></div>"
                    }
                    tempoutcardsstr+="</div>"
                }
                $('#Seat'+n.toString()+'_OutCardsDiv').append(tempoutcardsstr)
                $("#Seat" + n.toString() + "_CardButtonDiv").hide();
            }
            else {
                raiseError(result.result.substring(0,1));
                $("#Seat" + n.toString() + "_CardButtonDiv").show();
            }
        }
    });
}

function chooseconfirmdiscard(n,cardids,handcards)
{
    $('#Seat'+n.toString()+'_OutCardsDiv').empty();
    tempoutcardsstr="<div class='CurrentUserOutCards' style='top:-30px;width:150px;'><div id='confirmoutcardsdiv' style='float:left'><input class=\"btn1\" type=\"button\" value=\"确认\" onclick='confirmdiscard("+n.toString()+",\""+cardids+"\",\""+handcards+"\")'><input class=\"btn2\" type=\"button\" value=\"取消\" onclick='unconfirmdiscard("+n+",\""+cardids+"\",\""+handcards+"\")'></div></div><div class='CurrentUserOutCards' style='top:0px' onclick='chooseconfirmdiscard("+n.toString()+",\""+cardids+"\",\""+handcards+"\")'>"
    returncards=handcards.split(",");
    for(j=0;j<returncards.length;j++)
    {
        tempoutcardsstr+="<div class='CurrentUserOutCards' style='left:"+(j*14).toString()+"px;'><img src='/images/cards/id/"+returncards[j]+"_s.gif' alt='') /></div>"
        $('#Seat'+n.toString()+'_OutCardsDiv').append(tempoutcardsstr)
    }
    tempoutcardsstr+="</div>"
}

function unconfirmdiscard(n,cardids,handcards)
{
    $('#Seat'+n.toString()+'_OutCardsDiv').empty();
}

function confirmdiscard(n,cardids,handcards)
{
    $.ajax({
        url: 'ajaxroom.aspx',
        type: 'GET',
        dataType: 'json',
        contentType: "application/x-www-form-urlencoded; charset=utf-8", 
        timeout: 5000,
        data: "opName=Discard&roomguid=" + CurrentRoomGUID+"&cardids="+cardids+"&ArrageCardID="+handcards+"&RoundGUID="+CurrentRoundGUID+"&lasttype="+R_LastType+"&lastcardids="+R_LastArrageCardID+"&ran="+Math.random(),
        error: function() {
            $('#Seat'+n.toString()+'_OutCardsDiv').empty();
            $("#Seat" + n.toString() + "_CardButtonDiv").show();
        },
        success: function(result) {
            if (result.result.substring(0,1) == "0") {
                $("#Seat" + n.toString() + "_CardButtonDiv").hide();
                $('#Seat'+n.toString()+'_OutCardsDiv').empty();
                if(handcards!=result.result.substring(2))
                    return;
                returncards=handcards.split(",");
                for(j=0;j<returncards.length;j++)
                {
                    tempoutcardsstr="<div class='CurrentUserOutCards' style='left:"+(j*14).toString()+"px;'><img src='/images/cards/id/"+returncards[j]+"_s.gif' alt='') /></div>"
                    $('#Seat'+n.toString()+'_OutCardsDiv').append(tempoutcardsstr)
                }
                outcards=cardids.split(",");
                for(i=0;i<outcards.length;i++)
                {
                    $('#Seat'+n.toString()+'_CardCardsDiv').find("[cardid='"+outcards[i]+"']").remove();
                }
                $('#Seat' + n.toString() + '_CardButtonDiv').hide();
                $('#Seat' + n.toString() + '_CardButtonDiv_start').hide();
            }
            else {
                $('#Seat'+n.toString()+'_OutCardsDiv').empty();
                raiseError(result.result.substring(0,1));
                //$("#Seat" + n.toString() + "_CardButtonDiv").show();
            }
        }
    });
}

function Cardpass(n)
{
    //$("#Seat" + n.toString() + "_CardButtonDiv").hide();
    $.ajax({
        url: 'ajaxroom.aspx',
        type: 'GET',
        dataType: 'json',
        contentType: "application/x-www-form-urlencoded; charset=utf-8", 
        timeout: 5000,
        data: "opName=Pass&roomguid=" + CurrentRoomGUID+"&RoundGUID="+CurrentRoundGUID+"&ran="+Math.random(),
        error: function() {
            //$("#Seat" + n.toString() + "_CardButtonDiv").show();
        },
        success: function(result) {
            if (result.result == 0) {
                $("#Seat" + n.toString() + "_CardButtonDiv").hide();
//                $("#Seat" + n.toString() + "_PlayerDiv").hide();
//                $("#Seat" + n.toString() + "_ArrowDiv").show();
//                CurrentSeat = "-1";
            }
            else {
                raiseError(errorcode);
                $("#Seat" + n.toString() + "_CardButtonDiv").show();
            }
        }
    });
}

function CardStart(n)
{
    //$("#Seat" + n.toString() + "_CardButtonDiv").hide();
    $.ajax({
        url: 'ajaxroom.aspx',
        type: 'GET',
        dataType: 'json',
        contentType: "application/x-www-form-urlencoded; charset=utf-8", 
        timeout: 5000,
        data: "opName=Ready&roomguid=" + CurrentRoomGUID+"&ran="+Math.random(),
        error: function() {
            $("#Seat" + n.toString() + "_CardButtonDiv").show();
        },
        success: function(result) {
            if (result.result == 0) {
                $("#Seat" + n.toString() + "_CardButtonDiv").hide();
                //$("#Seat" + n.toString() + "_PlayerDiv").hide();
                // $("#Seat" + n.toString() + "_ArrowDiv").show();
                //CurrentSeat = "-1";
            }
            else {
                raiseError(result.result);
                $("#Seat" + n.toString() + "_CardButtonDiv").show();
            }
        }
    });
}

function CardStand(n) {
    $("#Seat" + n.toString() + "_CardButtonDiv").hide();
    $.ajax({
        url: 'ajaxroom.aspx',
        type: 'GET',
        dataType: 'json',
        contentType: "application/x-www-form-urlencoded; charset=utf-8", 
        timeout: 5000,
        data: "opName=Stand&roomguid=" + CurrentRoomGUID+"&ran="+Math.random(),
        error: function() {
            $("#Seat" + n.toString() + "_CardButtonDiv").show();
        },
        success: function(result) {
            if (result.result == 0) {
                $("#Seat" + n.toString() + "_PlayerDiv").hide();
                $("#Seat" + n.toString() + "_ArrowDiv").show();
                CurrentSeat = "-1";
            }
            else {
                raiseError(errorcode);
                $("#Seat" + n.toString() + "_CardButtonDiv").show();
            }
        }
    });
    
}

function sit(n) {
    $("#Seat" + n.toString() + "_ArrowDiv").hide();
    $.ajax({
        url: 'ajaxroom.aspx',
        type: 'GET',
        dataType: 'json',
        contentType: "application/x-www-form-urlencoded; charset=utf-8", 
        timeout: 5000,
        data: "opName=Sit&roomguid=" + CurrentRoomGUID + "&seatid=" + n.toString()+"&ran="+Math.random(),
        error: function() {
            $("#Seat" + n.toString() + "_ArrowDiv").show();
        },
        success: function(result) {
            if (result.result == 0) {
                $("#Seat" + n.toString() + "_ArrowDiv").hide();
                $("#Seat" + n.toString() + "_CardButtonDiv").show();
                $("#Seat" + n.toString() + "_PlayerDiv").show();
                CurrentSeat = n.toString();
            }
            else
                raiseError(result.result);
        }
    });
}

function CardMove(id)
{
    if($("#"+id).parent().css("top")=="-10px")
        $("#"+id).parent().css("top","0px")
    else
        $("#"+id).parent().css("top","-10px")
}

function raiseError(errorcode) {
    //不需要理会的错，直接return
    if (errorcode == "") {
        return;
    }
    if(errorcode == "-90026")
    {
        alert("房间专用游戏币不足，无法入坐")
        return;
    }
    ClearVersion();
}

function KeyPressChat(e)
{
    var keynum
    if(window.event) // IE
    {
        keynum = e.keyCode
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }
    if(keynum==13)
    {
        SendMsg();
        return false;
    }    
    return true;
}

function SendMsg()
{
    $.ajax({
        url: 'ajaxroom.aspx',
        type: 'GET',
        dataType: 'json',
        contentType: "application/x-www-form-urlencoded; charset=utf-8", 
        timeout: 5000,
        data: "opName=Chat&roomguid=" + CurrentRoomGUID + "&ChatContent=" + encodeURI($("#chatinputarea").val())+"&ran="+Math.random(),
        error: function() {
            alert("发送消息失败");
        },
        success: function(result) {
            if (result.result == 0) {
                 $("#chatinputarea").val("");
            }
            else
                raiseError(result.result);
        }
    });
}

function closeresult()
{
    $("#CenterResultDiv").hide();   
}

function ConvertDate(str)
{
    var strArray=str.split(" ");   
    var strDate=strArray[0].split("-");
    var strTime=strArray[1].split(":");
    return(new Date(strDate[0],(strDate[1]-parseInt(1)),strDate[2],strTime[0],strTime[1],strTime[2]))
}
